package klassen;

import java.io.IOException;
import java.sql.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WuerfelnServlet
 */
@WebServlet(name = "WuerfelnServlet", urlPatterns = {"/WuerfelnServlet"})
public class WuerfelnServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Wuerfel wuerfel = new Wuerfel();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WuerfelnServlet() {
        super();
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String user = request.getParameter("user");
		int dice1 = wuerfel.wuerfeln();
		int dice2 = wuerfel.wuerfeln();
		int position = wuerfel.newPosition(dice1, dice2, user);
		 try {
		out.println( "{" + "\"dice1\":"+dice1+"," + "\"dice2\":"+dice2+"," + "\"position\":"+position+"" + "}" );
	 } finally { out.close(); } 
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		int dice1 = wuerfel.wuerfeln();
		int dice2 = wuerfel.wuerfeln();
		 try {
		out.println( "{" + "\"dice1\":"+dice1+"," + "\"dice2\":"+dice2+"" + "}" );
	 } finally { out.close(); } 
		// TODO Auto-generated method stub
	
		// TODO Auto-generated method stub
	}

}
